#include <string.h>
#include <pthread.h>
#include <dirent.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include "getfilethread.h"
#include "dbfileQue.h"
#include "carQue.h"
#include "../sqlite3/ms_CarInfo_dataBase.h"
#include "../ms_resDeal.h"
#include "../main.h"

#ifdef UC_MS02

extern pthread_mutex_t 	g_opFilemutex;//互斥锁
extern char	g_opcarID[256];	//正在读取的xml文件绝对路径

typedef struct _SPathName
{
	long pathname_dd;
	char pathname[256];
}SPathName;//存储路径信息

static int m_exit_getFilethread = 1;
pthread_t       m_getFile_thread_t;         //获取文件信息线程
char m_tailID[CARLEMENT_NUM*2][20];//记录已经获取到的车辆信息时间(精确到毫秒)，防止重复获取
int m_tailIDIndex=0;
char m_dbname[DBFILEELEMNET_NUM*2][256];//记录已经扫描到的数据库文件名称，防止重复扫描
int m_dbIndex=0;
static time_t m_nocarsec;
static time_t m_nodbfilesec;

//拷贝文件夹名称并对文件夹进行排序，从旧到新
//stPathname-存储路径信息  pathcount-存储路径个数  onepath-拷贝路径“YYYYMMDDHH”  onepath_dd-拷贝路径表示时间
void strcpypath(SPathName *stPathname,int pathcount,char *onepath,long onepath_dd)
{
	int k = 0,i=0;

	if(onepath==NULL)
		return;
	for(i=0;i<pathcount;i++)
	{
		//如果是存储路径是空，直接存储
		if(strcmp(stPathname[i].pathname,"")==0)
		{
			strcpy(stPathname[i].pathname,onepath);
			stPathname[i].pathname_dd =  onepath_dd;
			//printf("cpy path:%s-%ld\n",stPathname[i].pathname,onepath_dd);
			break;
		}//如果已有存储路径，把要拷贝的路径放到合适的位置，顺序是从小到大
		else if(onepath_dd<stPathname[i].pathname_dd)
		{
			//拷贝文件夹名称
			for(k=pathcount-1;k>i;k--)
			{
				stPathname[k]=stPathname[k-1];
			}
			//存储当前目录
			strcpy(stPathname[i].pathname,onepath);
			//printf("cpy path:%s-%ld\n",stPathname[i].pathname,onepath_dd);
			stPathname[i].pathname_dd =  onepath_dd;
			break;
		}
	}

}

//递归查找符合“YYYYMMDD”格式的文件夹，个数是pathcount
//传出函数：stPathname
//返回值：查找到的有效文件夹个数
int getYYYYDiskPath(char *path,SPathName *stPathname,int pathcount)
{
	DIR *dproot=NULL;//根目录之下的文件
	struct dirent *rootdir;
	DIR *subroot=NULL;//子目录之下的文件
	struct dirent *subdir;
	char emptyfilebuff[100]={0};//查询空文件夹路径的缓冲区
	int count=0,ret=0,sysflag=0;//子目录中的文件数量
	long filename_dd = 0;
	char nowday[100]={0};
	//time_t timep;
	struct tm *p;
	char temp[200]={0};
	//time(&timep);
	//p=gmtime(&timep);
	//getlocaltime(&p);
	struct timeval tv;
	struct timezone tz;
	struct tm  timeSc;//*gtm,
	int timeret = 0;
	timeret = gettimeofday(&tv,&tz);
	if(timeret == -1)
	{
		printf("[Function] getlocaltime error:");
	}
	tv.tv_sec +=  8*60*60;	//获取的是GMT时间 转为本地时间
	gmtime_r(&(tv.tv_sec), &timeSc);
	p = &timeSc;
	sprintf(nowday,"%04d%02d%02d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
	//printf("getYYYYDiskPath now day %s\n",nowday);

	//遍历根目录
	dproot = opendir(path);
	if (!dproot)
	{
	   printf("getYYYYDiskPath open directory error=%d-%s\n",errno,path);
	   return 0;
	}
//	printf("opendir 1:%s \n",path);
	//查找“年月日时”文件夹
	while ((rootdir=readdir(dproot)))
	{
		usleep(10);
		if(strcmp(rootdir->d_name,".")==0||strcmp(rootdir->d_name,"..")==0||strcmp(rootdir->d_name,"")==0)
			continue;
		memset(emptyfilebuff,0,sizeof(emptyfilebuff));
		count=0;
		//文件夹转换为时间值
		filename_dd = atol(rootdir->d_name);
		//绝对路径
		sprintf(emptyfilebuff,"%s/%s",path,rootdir->d_name);
		//printf("####find pathname=%s\n",emptyfilebuff);
		//如果是“年月日时”文件夹，如果是空文件夹，删除 1970010100 2999123123
		if(strlen(rootdir->d_name)==8 && (filename_dd >= 19700101) && (filename_dd<=29991231))
		{
			subroot=opendir(emptyfilebuff);
			if (!subroot)
			{
			   printf("getYYYYDiskPath2 open directory error=%d-%s\n",errno,emptyfilebuff);
			   printf("dir error %d %d %d %d %d\n",EMFILE,ENFILE,ENOTDIR,ENOENT,ENOMEM);
			   continue;
			}
			//printf("opendir 2:%s \n",emptyfilebuff);
			while ((subdir=readdir(subroot)))
			{
				if(strcmp(subdir->d_name,".")==0||strcmp(subdir->d_name,"..")==0||strcmp(subdir->d_name,"")==0)
					continue;
				count++;
				if(count>0)
					break;
			}
			closedir(subroot);
			//				printf("closedir 2:%s \n",emptyfilebuff);
			if(count==0)//是空文件夹，删除这个子目录
			{
				//如果是当天的数据，不进行删除操作
				if(filename_dd!=atol(nowday))
				{
					printf("getYYYYDiskPath remove dir %ld!=%s %d-%s\n",filename_dd,nowday,ret,emptyfilebuff);
					sprintf(temp,"rm -rf %s",emptyfilebuff);
					sysflag = system(temp);
					if(sysflag!=0)
						printf("rm failed %d %s\n",sysflag,strerror(errno));
					sleep(1);
				}
			}
			else//保存文件夹名
			{
				ret++;
				strcpypath(stPathname,pathcount,emptyfilebuff,filename_dd);
			}
		}else
		{
			ret+=getYYYYDiskPath(emptyfilebuff,stPathname,pathcount);
		}
	}
	//关闭
	closedir(dproot);
//	printf("closedir 1:%s \n",path);
	return ret;
}
//查找最旧小时的文件夹目录
int findHHDirPath(char *path,long pathnum,SPathName *stPathname,int pathcount)
{
	DIR *dproot=NULL;//根目录之下的文件
	struct dirent *rootdir;
	DIR *subroot=NULL;//子目录之下的文件
	struct dirent *subdir;
	char emptyfilebuff[256]={0};//查询空文件夹路径的缓冲区
	char nowday[100]={0};
	int filename_dd = 0,count=0;
	char cmd[256]={0};
	//time_t timep;
	int dircount = 0;
	struct tm *p;
	//time(&timep);
	//gmtime_r(&timep,&p);
	//getlocaltime(&p);
	struct timeval tv;
	struct timezone tz;
	struct tm  timeSc;//*gtm,
	int ret = 0;
	ret = gettimeofday(&tv,&tz);
	if(ret == -1)
	{
		printf("[Function] getlocaltime error:");
	}
	tv.tv_sec +=  8*60*60;	//获取的是GMT时间 转为本地时间
	gmtime_r(&(tv.tv_sec), &timeSc);
	p = &timeSc;
	sprintf(nowday,"%04d%02d%02d/%02d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday,p->tm_hour);
	//printf(" 2015-2-4 findHHDirPath %s\r\n",nowday);

	//遍历根目录
	dproot = opendir(path);
	if (!dproot)
	{
	   printf("findHHDirPath open directory error-%s\n",path);
	   return 0;
	}
//	printf("opendir 3:%s \n",path);
	//查找“年月日时”文件夹
	while ((rootdir=readdir(dproot)))
	{
		usleep(1);
		count = 0;
		if(strcmp(rootdir->d_name,".")==0||strcmp(rootdir->d_name,"..")==0||strcmp(rootdir->d_name,"")==0)
			continue;
		//00~23范围的文件夹
		filename_dd = atoi(rootdir->d_name);
		sprintf(emptyfilebuff,"%s/%s",path,rootdir->d_name);
		//tftpser正在读取mid对应的文件，防止删除线程删除文件，所以记录id
		pthread_mutex_lock(&g_opFilemutex);
		if(strlen(g_opcarID)>0 && strcmp(g_opcarID,emptyfilebuff)==0)
		{
			printf("del path,not serch:%s----%s\n",g_opcarID,emptyfilebuff);
			pthread_mutex_unlock(&g_opFilemutex);
			continue;
		}
		pthread_mutex_unlock(&g_opFilemutex);

		//printf("findHHDirPath %s\r\n",emptyfilebuff);
		if(strlen(rootdir->d_name)==2 && (filename_dd >= 0) && (filename_dd<=23))
		{
			//如果是空文件夹，删除文件夹，如果不是，对文件夹进行排序
			subroot=opendir(emptyfilebuff);
			if (!subroot)
			{
			   printf("findHHDirPath2 open directory error-%s\n",emptyfilebuff);
			   continue;
			}
//			printf("opendir 4:%s \n",emptyfilebuff);
			while ((subdir=readdir(subroot)))
			{
				if(strcmp(subdir->d_name,".")==0||strcmp(subdir->d_name,"..")==0||strcmp(subdir->d_name,"")==0)
					continue;
				count++;
				if(count>0)
					break;
			}
			closedir(subroot);
//				printf("closedir 4:%s \n",emptyfilebuff);
			if(count==0)//是空文件加，删除这个子目录
			{
				//如果是当天的数据，不进行删除操作
				//printf("strstr=%s\n",strstr(path,nowday));
				if(strstr(emptyfilebuff,nowday)==0)
				{
					printf("findHHDirPath remove dir %s!=%s\n",emptyfilebuff,nowday);
					sprintf(cmd,"rm -rf %s",emptyfilebuff);
					filename_dd = system(cmd);
					if(filename_dd!=0)
						printf("%s failed %d:%s\n",cmd,filename_dd,strerror(errno));

					sleep(1);
				}
			}
			else//保存文件夹名
			{
				//printf("%d %d\n",pathnum,(pathnum%10000)*100);
				strcpypath(stPathname,pathcount,emptyfilebuff,(filename_dd+(pathnum%10000)*100));
				dircount++;
			}
		}else
		{
			printf("findHHDirPath del invailed pathname %s/%s\r\n",path,rootdir->d_name);
			sprintf(cmd,"rm -rf %s",emptyfilebuff);
			filename_dd = system(cmd);
			if(filename_dd!=0)
				printf("%s failed %d:%s\n",cmd,filename_dd,strerror(errno));
			sleep(1);
		}
	}
	closedir(dproot);
//	printf("closedir 3:%s \n",path);
	return dircount;
}
/*
 * 函数作用：递归遍历目录查找最老的N个记录
 * 参数：buff 查询到的结果的路径 ，path 根路径
 * 处理逻辑：查找目录下xml文件，并解析xml获取图片名称；
 * 返回值:查找文件个数
 * 注意：只能查找多个目录并且只有一个子目录下有文件的目录结构
 */
int findcarfile(char *path,SCarFileInfo *carfileinfo,int filecount)
{
	DIR *subroot;//子目录之下的文件
	struct dirent *subdir;
	int count=0,overflag=0,j=0,i=0,real_file_num=0,xmlflag=0,ret=0;//文件数量
	SCarFileInfo tempres;//查找文件信息
	SPathName filename[2];
	SPathName pathname_dd[2];
	char searchpath[256]={0};//组织查询文件目录的路径
	char temp[256]={0};
	//time_t timep;
	struct tm *p;
	char nowday[20]={0};
	//time(&timep);
	//p=gmtime(&timep);
	struct timeval tv;
	struct timezone tz;
	struct tm  timeSc;//*gtm,
	int timeret = 0;
	timeret = gettimeofday(&tv,&tz);
	if(timeret == -1)
	{
		printf("[Function] getlocaltime error:");
	}
	tv.tv_sec +=  8*60*60;	//获取的是GMT时间 转为本地时间
	gmtime_r(&(tv.tv_sec), &timeSc);
	p = &timeSc;
	sprintf(nowday,"%04d%02d%02d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
	//printf("findcarfile nowday %s\n",nowday);
	//查找文件
	char emptyfilebuff[256]={0};//查询空文件夹路径的缓冲区
	for(i=0;i<2;i++)
		memset(&filename[i],0,sizeof(SPathName));
	for(i=0;i<2;i++)
		memset(&pathname_dd[i],0,sizeof(SPathName));

	//递归查找符合“YYYYMMDD”格式的文件夹,并排序 --2个
	if( 0>=getYYYYDiskPath(path,pathname_dd,2))
	{
		printf("there is no data\n");
		return 0;
	}
	//查找filecount个最旧的小时文件夹--2个
	while((real_file_num<2) && (j<2))
	{
		//printf("pathname_dd[%d].pathname=%s\n",j,pathname_dd[j].pathname);
		if(strcmp(pathname_dd[j].pathname,"")==0)
		{
			//printf("empty file:%s\n",pathname_dd[j].pathname);
			j++;
			continue;
		}
		real_file_num+= findHHDirPath(pathname_dd[j].pathname,pathname_dd[j].pathname_dd,filename,2);
		j++;
	}


	if(real_file_num==0)
	{
		printf("there is no hour data\n");
		return 0;
	}
	//查找最旧的文件filecount个
	j=0;
	while((count<filecount) && (j<real_file_num) &&(j<2))
	{
		if(strcmp(filename[j].pathname,"")==0)
		{
			//printf("empty file:%s\n",filename[j].pathname);
			j++;
			continue;
		}
		memset(searchpath,0,sizeof(searchpath));
		xmlflag = 0;//文件夹中是否存在xml的标志
		sprintf(searchpath,"%s/",filename[j].pathname);
		subroot=opendir(searchpath);
		if (!subroot)
		{
		   printf("open directory error-%s\n",searchpath);
		   continue;
		}
		//printf("opendir:5 %s\n",searchpath);
		while((subdir=readdir(subroot)) && count<filecount)
		{
			if(strcmp(subdir->d_name,".")==0||strcmp(subdir->d_name,"..")==0||strcmp(subdir->d_name,"")==0)
				continue;
			overflag = 0;
			if(strstr(subdir->d_name,".xml")==0)
				continue;
			xmlflag = 1;//存在xml文件
			//判断文件后缀名称，只取用后缀时“.xml”的文件
			//printf("findday:%s\n",subdir->d_name);
			overflag = 0;
			for(i=0;i<CARLEMENT_NUM*2;i++)
			{
				if((strlen(m_tailID[i])>0) &&(strncmp(m_tailID[i],subdir->d_name,strlen(m_tailID[i]))==0))
				{
					//printf("found over carfile[%d]:%s\n",i,subdir->d_name);
					i=CARLEMENT_NUM*2+1;
					overflag = 1;
				}
			}
			//如果文件被查找到过
			if(1==overflag)
				continue;
			//组织文件绝对路径
			sprintf(emptyfilebuff,"%s/%s",searchpath,subdir->d_name);
			memset(&tempres,0,sizeof(SCarFileInfo));
			//解析xml，获取时间id、大图、小图、xml文件名称
			if(0==Xml_JieXiCarXml(emptyfilebuff,&tempres))
			{
				printf("Xml_JieXiCarXml fail %s\r\n",emptyfilebuff);
				remove(emptyfilebuff);//解析文件失败，删除文件，否则可能影响后面文件的处理
			}
			//组织文件绝对路径
			memcpy(carfileinfo[count].timeid,tempres.timeid,strlen(tempres.timeid));
			if(strlen(tempres.bigPicPath))
			{
				g2u(tempres.bigPicPath,strlen(tempres.bigPicPath),temp,sizeof(temp));
				sprintf(carfileinfo[count].bigPicPath,"%s%s",searchpath,temp);
			}
			if(strlen(tempres.smallPicPath))
			{
				g2u(tempres.smallPicPath,strlen(tempres.smallPicPath),temp,sizeof(temp));
				sprintf(carfileinfo[count].smallPicPath,"%s%s",searchpath,temp);
			}
			if(strlen(tempres.xmlPath))
			{
				g2u(tempres.xmlPath,strlen(tempres.xmlPath),temp,sizeof(temp));
				sprintf(carfileinfo[count].xmlPath,"%s%s",searchpath,temp);
			}

			count++;
		}
		closedir(subroot);
		//如果文件夹中没有“.xml”的文件，删除这个文件夹
		if(xmlflag==0)
		{
			if(strstr(filename[j].pathname,nowday)==0)
			{
				printf("this dir no xml file:%s:%s\n",filename[j].pathname,nowday);
				sprintf(temp,"rm -rf %s",filename[j].pathname);
				//printf(temp);
				ret=system(temp);
				if(ret!=0)
					printf("rm failed %d %s\n",ret,strerror(errno));
				sleep(1);
			}
		}
//		printf("closedir:5 %s\n",searchpath);
		j++;
	}
//	printf("find car file over\n");
	return count;
}
//获取车辆信息文件
void getcarfile()
{
	//车辆信息结构
	SCarFileInfo carfileinfo[CARLEMENT_NUM];
	int i = 0;
	int ret = 0;
	int carcount = 0;
	//上次没有查找到数据，间隔时间长一点再查找，否则影响tftp传输
	if((time((time_t*)NULL))-m_nocarsec<=5)
	{
		//printf("get car time waite %ld\n",(time((time_t*)NULL))-m_nocarsec);
		return;
	}

	//队列满
	carcount = carqueue_GetFreeSize();
	//减少查找数据库次数，查找数据库耗时较多
	if(carcount<=0)
	{
		//printf("get tailcount size %d\n",carcount);
		return;
	}
	//printf("get tailcount %d\n",carcount);
	if(carcount>(CARLEMENT_NUM-1))
		carcount = CARLEMENT_NUM-1;
	for(i=0;i<carcount;i++)
		memset(&carfileinfo[i],0,sizeof(SCarFileInfo));
{
	struct timeval st,ed;
	gettimeofday(&st,NULL);
	//根据目录查找，获取车辆文件信息
	ret = findcarfile(g_dirPathPic,carfileinfo,carcount);
	if((ret<=0) ||(carcount==0))
	{
		printf("get car info from disk failed,num=%d,ret=%d\n",carcount,ret);
		gettimeofday(&ed,NULL);
		//printf("$$$$$get car info from disk time:%ld ms\n",(ed.tv_sec-st.tv_sec)*1000+(ed.tv_usec-st.tv_usec)/1000);
		m_nocarsec = time((time_t*)NULL);
		return;
	}
	gettimeofday(&ed,NULL);
	//printf("$$$$$get car info from disk time:%ld ms\n",(ed.tv_sec-st.tv_sec)*1000+(ed.tv_usec-st.tv_usec)/1000);
}
	for(i=0;i<ret;i++)
	{
		if(carqueue_GetFreeSize()>0)
		{
			if(m_tailIDIndex==CARLEMENT_NUM*2)
			{
				m_tailIDIndex=0;
			}
			sprintf(m_tailID[m_tailIDIndex],"%s",carfileinfo[i].timeid);//记录已经放到记录时间id数组中
			m_tailIDIndex++;
			printf("put data in carque %s \n",/*carfileinfo[i].timeid,*/carfileinfo[i].xmlPath);
			carqueue_PutData(&carfileinfo[i]);
		}else
			break;
	}
}
/*
 * 函数作用：递归遍历目录查找最老的N个记录
 * 参数：buff 查询到的结果的路径 ，path 根路径
 * 返回值:查找文件个数
 * 注意：只能查找多个目录并且只有一个子目录下有文件的目录结构
 */
int findtrafficdb(char *path,SDBfileFileInfo *buff,int filecount)
{
	DIR *dproot=NULL;//根目录之下的文件
	struct dirent *rootdir;
	//time_t timep;
	struct tm *p;
	int count=0,overflag=0,i;//文件数量
	//查找数据库文件
	char emptyfilebuff[100]={0};//查询空文件夹路径的缓冲区
	char nowday[100]={0};

	//time(&timep);
	//p=gmtime(&timep);
	struct timeval tv;
	struct timezone tz;
	struct tm  timeSc;//*gtm,
	int timeret = 0;
	timeret = gettimeofday(&tv,&tz);
	if(timeret == -1)
	{
		printf("[Function] getlocaltime error:");
	}
	tv.tv_sec +=  8*60*60;	//获取的是GMT时间 转为本地时间
	gmtime_r(&(tv.tv_sec), &timeSc);
	p = &timeSc;
	sprintf(nowday,"%02d%02d%02d",(1900+p->tm_year)%100, (1+p->tm_mon),p->tm_mday);
	//printf("findtrafficdb nowday %s\n",nowday);

	//遍历目录
	dproot = opendir(path);
//	printf("open dir 6:%s\n",path);
	if (NULL==dproot)
	{
	   printf("findtrafficdb open directory error-%s\n",path);
	   return 0;
	}
	//printf("open directory -%d  %s\n",dproot,path);
	//查找数据库文件
	while ((rootdir=readdir(dproot)) && (count<filecount))
	{
		if(rootdir==NULL)
			break;

		if(strcmp(rootdir->d_name,".")==0||strcmp(rootdir->d_name,"..")==0||strcmp(rootdir->d_name,"")==0)
			continue;
		//如果是文件夹，因为在本系统中d_type不支持，所以用文件后缀区分
		if(strstr(rootdir->d_name,".db")==0)//if(rootdir->d_type==DT_DIR)
		{
			//printf("rootdir->d_name=%s\n",rootdir->d_name);
			//文件夹转换为时间值
			sprintf(emptyfilebuff,"%s/%s",path,rootdir->d_name);
			count+=findtrafficdb(emptyfilebuff,buff,filecount);
		}else
		{
			memset(emptyfilebuff,0,sizeof(emptyfilebuff));
			//文件夹转换为时间值
			memcpy(emptyfilebuff,&(rootdir->d_name[8]),6);
			//不上传当天的数据库文件
			//if(atol(nowday)==atol(emptyfilebuff))
			if(strstr(rootdir->d_name,nowday)!=0)
			{
				printf("nowday:%s,findday:%s\n",nowday,emptyfilebuff);
				continue;
			}
			overflag = 0;
			for(i=0;i<DBFILEELEMNET_NUM*2;i++)
			{
				if((strlen(m_dbname[i])>0) &&(strncmp(m_dbname[i],rootdir->d_name,strlen(m_dbname[i]))==0))
				{
					//printf("found over dbfile[%d]:%s\n",i,rootdir->d_name);
					i=DBFILEELEMNET_NUM*2+1;
					overflag = 1;
					continue;
				}
			}
			//如果文件没有被查找到过
			if(0==overflag)
			{
				//绝对路径
				sprintf(buff[count].dbPath,"%s/%s",path,rootdir->d_name);
				memcpy(buff[count].dbname,&(buff[count].dbPath[strlen(DB_PATH)]),strlen(buff[count].dbPath)-strlen(DB_PATH));
				//printf("name%s\n path=%s\n",buff[count].dbname,buff[count].dbPath);
				sprintf(m_dbname[m_dbIndex],"%s",rootdir->d_name);
				m_dbIndex++;
				if(m_dbIndex==DBFILEELEMNET_NUM*2)
					m_dbIndex = 0;
				count++;
			}
		}
	}
	//关闭
	closedir(dproot);
//	printf("close dir 6:%s\n",path);
//	printf("find db file over\n");

	return count;
}
//函数功能：获取数据库文件
//	1.上次没有查找到数据，间隔时间长一点再查找，否则影响tftp传输
//	2.队列剩余空间小于2，不进行扫描；减少递归查找次数；队列只有一个数据为空，但是第一个文件夹中文件都被放到队列中，会一直查找
//	3.
void gettrafficdb()
{
	int count=0;
	int dbcount = 0;
	int i = 0;
	//char filepath[DBFILEELEMNET_NUM][256];
	SDBfileFileInfo dbfileinfo[DBFILEELEMNET_NUM];

	//上次没有查找到数据，间隔时间长一点再查找，否则影响tftp传输
	if(time((time_t*)NULL)-m_nodbfilesec<=36)
		return;
	//队列满
	dbcount = dbfilequeue_GetFreeSize();
	//减少递归查找次数；队列只有一个数据为空，但是第一个文件夹中文件都被放到队列中，会一直查找
	if(dbcount<1)
		return;
//	printf("$$$$$$$$$$$$$$$$get db file count %d\n",dbcount);
	if(dbcount>(DBFILEELEMNET_NUM))
		dbcount =DBFILEELEMNET_NUM;
	for(i=0;i<DBFILEELEMNET_NUM;i++)
		memset(&dbfileinfo[i],0,sizeof(SDBfileFileInfo));
	//查询流量信息文件
	count= findtrafficdb(DB_PATH,dbfileinfo,dbcount);
	if(count<=0)
	{
		printf("####there is no db file in disk \n");
		m_nodbfilesec = time((time_t*)NULL);
		return;
	}
	printf("db file found count is %d\n",count);
	for(i=0;i<count;i++)
	{
		//printf("put db file %s,%s\n",dbfileinfo[i].dbPath,dbfileinfo[i].dbname);
		if(dbfilequeue_GetFreeSize()>0)
			dbfilequeue_PutData(&dbfileinfo[i]);
		else
			break;
	}
}
//线程函数
void getFile_Proc ()
{
	m_nocarsec = time((time_t*)NULL)-600;
	m_nodbfilesec = time(NULL)-3600;
	while(m_exit_getFilethread != 1)
	{
		MainTheaddogSet(5);
		usleep(1);
#if USE_SD
		//	SysInfo *sysinfo=NULL;
		//检验是否插入SD卡，没有，不保存文件
		sysinfo = GetSysInfo();
		if(sysinfo==NULL || sysinfo->sdcard_config.sdinsert != 0x03)
		{
			printf("sysinfois null or sysinfo->sdcard_config.sdinsert != 0x03\n");
			usleep(50000);
			continue;
		}
#endif
		//获取车辆信息文件
		getcarfile();
		//获取数据库文件
		gettrafficdb();
	}
}

//启动线程
void getfilethread_start()
{
	int i=0;
	m_exit_getFilethread = 0;

	//初始化车辆信息队列
	carqueue_init();
	//数据库文件队列初始化
	dbfilequeue_init();
	for(i=0;i<CARLEMENT_NUM*2;i++)
		memset(m_tailID[i],0,sizeof(m_tailID[i]));
	m_tailIDIndex=0;
	m_dbIndex = 0;
	for(i=0;i<DBFILEELEMNET_NUM*2;i++)
		memset(m_dbname[i],0,256);

	//创建线程  开启TFTP服务
	if((pthread_create(&m_getFile_thread_t, NULL, (void*)&getFile_Proc, NULL)) != 0)
	{
		printf("********** getfile thread failed!\n");
		return;
	}
	printf("******** getfile thread success!\n");
}

void getfilethread_exit()
{
	m_exit_getFilethread = 1;

	usleep(10);
	if (pthread_join(m_getFile_thread_t, NULL) != 0)
	{
		printf("************ getfile thread pthread_join failed\n");
	}else
		printf("************ getfile thread pthread_join success\n");

	//车辆信息队列释放
	carqueue_free();
	//流量数据文件队列释放
	dbfilequeue_free();
}

#endif // UC_MS02
